package com.yingsheng.bi.hks.web.controller;

import com.yingsheng.bi.hks.basic.enums.XjyOssEnum;
import com.yingsheng.bi.hks.repository.dao.po.ServiceDetail;
import com.yingsheng.bi.hks.repository.dao.service.SequenceDaoService;
import com.yingsheng.bi.hks.repository.dao.service.ServiceDetailDaoService;
import io.minio.ObjectWriteResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(value = "图片更新")
public class Test {

    @Autowired
    private ServiceDetailDaoService serviceDetailDaoService;

    @ApiOperation(httpMethod = "POST", value = "详情图片更新")
    @RequestMapping(value = "/test/images1")
    public void add(String fileNameStr) {
        File dir = new File(fileNameStr);

        System.out.println(dir.getName());
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            assert files != null;
            List<String> objectList = new ArrayList<>();
            ServiceDetail serviceDetail = new ServiceDetail();
            for (File file : files)  {
                if (file.isFile()) {
                    String fileName = file.getName();
                    ObjectWriteResponse response = XjyOssEnum.SERVICE_DETAIL_IMAGE.putObject(file, fileName);
                    String objectName = response.object();
                    System.out.println(objectName);
                    if (fileName.contains("_t")) {
                        serviceDetail.setTopimages("http://cdn.nbyingchuang.cn/miniapp-xjy-dev"+objectName);
                    } else {
                        objectList.add("http://cdn.nbyingchuang.cn/miniapp-xjy-dev"+objectName);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(objectList)) {
                String images = StringUtils.collectionToCommaDelimitedString(objectList);
                serviceDetail.setId(dir.getName());
                serviceDetail.setImages(images);
                serviceDetailDaoService.updateById(serviceDetail);
            }
        }
        System.out.println("文件传输完毕");
    }


    @ApiOperation(httpMethod = "POST", value = "分类图片更新")
    @RequestMapping(value = "/test/images2")
    public void add2(String fileNameStr) {
        File dir = new File(fileNameStr);

        System.out.println(dir.getName());
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            assert files != null;
            List<String> objectList = new ArrayList<>();
            for (File file : files)  {
                if (file.isFile()) {
                    String fileName = file.getName();
                    ObjectWriteResponse response = XjyOssEnum.GOODS_IMAGE.putObject(file, fileName);
                    String objectName = response.object();
                    System.out.println(objectName);
                    objectList.add(objectName);
                }
            }
        }
        System.out.println("文件传输完毕");
    }

    @ApiOperation(httpMethod = "POST", value = "home")
    @RequestMapping(value = "/test/images3")
    public void add3(String fileNameStr) {
        File dir = new File(fileNameStr);

        System.out.println(dir.getName());
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            assert files != null;
            List<String> objectList = new ArrayList<>();
            for (File file : files)  {
                if (file.isFile()) {
                    String fileName = file.getName();
                    ObjectWriteResponse response = XjyOssEnum.HOME_IMAGE.putObject(file, fileName);
                    String objectName = response.object();
                    System.out.println(objectName);
                    objectList.add(objectName);
                }
            }
        }
        System.out.println("文件传输完毕");
    }


    @Autowired
    private SequenceDaoService sequenceDaoService;

    @ApiOperation(httpMethod = "POST", value = "序列")
    @RequestMapping(value = "/test/getSeq")
    public void getSeq(String fileNameStr) {
        sequenceDaoService.getNextVal("job_bh");
    }
}
